using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Entity;
using APP.QUANLY.Entity.ConditionForm;

namespace APP.QUANLY.DAL
{
    /// <summary>
    /// Summary description for TEMP_HOADONNHAPKHO.
     /// </summary>
	public class TEMP_HOADONNHAPKHODao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public TEMP_HOADONNHAPKHODao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public TEMP_HOADONNHAPKHODao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT MAHOADONNHAP, THANHTIENCHUACK_HD, CHIETKHAUTHEOTIEN_HD, SOTIENTHANHTOAN_HD, SOTIENCHI, LOAIHOADON, HANTHANHTOAN, GHICHU_HD, DATHANHTOAN, NGUOITAO, NGAYTAOHOADON, ISDELETE, MANHACUNGCAP, MACA FROM TEMP_HOADONNHAPKHO WHERE (MAHOADONNHAP = @MAHOADONNHAP)";
	        _SQLSelectByKey = query;

            query = @"UPDATE TEMP_HOADONNHAPKHO SET THANHTIENCHUACK_HD = @THANHTIENCHUACK_HD, CHIETKHAUTHEOTIEN_HD = @CHIETKHAUTHEOTIEN_HD, SOTIENTHANHTOAN_HD = @SOTIENTHANHTOAN_HD, SOTIENCHI = @SOTIENCHI, LOAIHOADON = @LOAIHOADON, HANTHANHTOAN = @HANTHANHTOAN, GHICHU_HD = @GHICHU_HD, DATHANHTOAN = @DATHANHTOAN, NGUOITAO = @NGUOITAO, NGAYTAOHOADON = @NGAYTAOHOADON, ISDELETE = @ISDELETE, MANHACUNGCAP = @MANHACUNGCAP, MACA = @MACA WHERE (MAHOADONNHAP = @MAHOADONNHAP)";
	        _SQLUpdate = query;

            query = @"INSERT INTO TEMP_HOADONNHAPKHO VALUES (@MAHOADONNHAP, @THANHTIENCHUACK_HD, @CHIETKHAUTHEOTIEN_HD, @SOTIENTHANHTOAN_HD, @SOTIENCHI, @LOAIHOADON, @HANTHANHTOAN, @GHICHU_HD, @DATHANHTOAN, @NGUOITAO, @NGAYTAOHOADON, @ISDELETE, @MANHACUNGCAP, @MACA)";
	        _SQLInsert = query;

            query = @"DELETE FROM TEMP_HOADONNHAPKHO WHERE (MAHOADONNHAP = @MAHOADONNHAP)";
	        _SQLDelete = query;

            query = @"SELECT MAHOADONNHAP, THANHTIENCHUACK_HD, CHIETKHAUTHEOTIEN_HD, SOTIENTHANHTOAN_HD, SOTIENCHI, LOAIHOADON, HANTHANHTOAN, GHICHU_HD, DATHANHTOAN, NGUOITAO, NGAYTAOHOADON, ISDELETE, MANHACUNGCAP, MACA FROM TEMP_HOADONNHAPKHO";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(TEMP_HOADONNHAPKHO obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "MAHOADONNHAP", obj.MAHOADONNHAP, DbType.String);
                         DalTools.addDbDataParameter(command, "THANHTIENCHUACK_HD", obj.THANHTIENCHUACK_HD, DbType.Decimal);
         DalTools.addDbDataParameter(command, "CHIETKHAUTHEOTIEN_HD", obj.CHIETKHAUTHEOTIEN_HD, DbType.Decimal);
         DalTools.addDbDataParameter(command, "SOTIENTHANHTOAN_HD", obj.SOTIENTHANHTOAN_HD, DbType.Decimal);
         DalTools.addDbDataParameter(command, "SOTIENCHI", obj.SOTIENCHI, DbType.Decimal);
         DalTools.addDbDataParameter(command, "LOAIHOADON", obj.LOAIHOADON, DbType.Int32);
         DalTools.addDbDataParameter(command, "HANTHANHTOAN", obj.HANTHANHTOAN, DbType.DateTime);
         DalTools.addDbDataParameter(command, "GHICHU_HD", obj.GHICHU_HD, DbType.String);
         DalTools.addDbDataParameter(command, "DATHANHTOAN", obj.DATHANHTOAN, DbType.Boolean);
         DalTools.addDbDataParameter(command, "NGUOITAO", obj.NGUOITAO, DbType.String);
         DalTools.addDbDataParameter(command, "NGAYTAOHOADON", obj.NGAYTAOHOADON, DbType.DateTime);
         DalTools.addDbDataParameter(command, "ISDELETE", obj.ISDELETE, DbType.Boolean);
         DalTools.addDbDataParameter(command, "MANHACUNGCAP", obj.MANHACUNGCAP, DbType.Int32);
         DalTools.addDbDataParameter(command, "MACA", obj.MACA, DbType.Int32);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(TEMP_HOADONNHAPKHO obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "MAHOADONNHAP", obj.MAHOADONNHAP, DbType.String);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(TEMP_HOADONNHAPKHO obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(TEMP_HOADONNHAPKHO obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private TEMP_HOADONNHAPKHOList SelectListByCommand(IDbCommand command)
        {
            TEMP_HOADONNHAPKHOList list = new TEMP_HOADONNHAPKHOList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				TEMP_HOADONNHAPKHO obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new TEMP_HOADONNHAPKHO();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private TEMP_HOADONNHAPKHOList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            TEMP_HOADONNHAPKHOList list = new TEMP_HOADONNHAPKHOList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				TEMP_HOADONNHAPKHO obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new TEMP_HOADONNHAPKHO();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(TEMP_HOADONNHAPKHO obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["MAHOADONNHAP"] is DBNull))
		{
			obj.MAHOADONNHAP = (string)reader["MAHOADONNHAP"];
		}
                if(!(reader["THANHTIENCHUACK_HD"] is DBNull))
		{
			obj.THANHTIENCHUACK_HD = (decimal)reader["THANHTIENCHUACK_HD"];
		}
                if(!(reader["CHIETKHAUTHEOTIEN_HD"] is DBNull))
		{
			obj.CHIETKHAUTHEOTIEN_HD = (decimal)reader["CHIETKHAUTHEOTIEN_HD"];
		}
                if(!(reader["SOTIENTHANHTOAN_HD"] is DBNull))
		{
			obj.SOTIENTHANHTOAN_HD = (decimal)reader["SOTIENTHANHTOAN_HD"];
		}
                if(!(reader["SOTIENCHI"] is DBNull))
		{
			obj.SOTIENCHI = (decimal)reader["SOTIENCHI"];
		}
                if(!(reader["LOAIHOADON"] is DBNull))
		{
			obj.LOAIHOADON = (Int32)reader["LOAIHOADON"];
		}
                if(!(reader["HANTHANHTOAN"] is DBNull))
		{
			obj.HANTHANHTOAN = (DateTime)reader["HANTHANHTOAN"];
		}
                if(!(reader["GHICHU_HD"] is DBNull))
		{
			obj.GHICHU_HD = (string)reader["GHICHU_HD"];
		}
                if(!(reader["DATHANHTOAN"] is DBNull))
		{
			obj.DATHANHTOAN = (bool)reader["DATHANHTOAN"];
		}
                if(!(reader["NGUOITAO"] is DBNull))
		{
			obj.NGUOITAO = (string)reader["NGUOITAO"];
		}
                if(!(reader["NGAYTAOHOADON"] is DBNull))
		{
			obj.NGAYTAOHOADON = (DateTime)reader["NGAYTAOHOADON"];
		}
                if(!(reader["ISDELETE"] is DBNull))
		{
			obj.ISDELETE = (bool)reader["ISDELETE"];
		}
                if(!(reader["MANHACUNGCAP"] is DBNull))
		{
			obj.MANHACUNGCAP = (Int32)reader["MANHACUNGCAP"];
		}
                if(!(reader["MACA"] is DBNull))
		{
			obj.MACA = (Int32)reader["MACA"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public TEMP_HOADONNHAPKHO SelectByKey(string MAHOADONNHAP)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MAHOADONNHAP", MAHOADONNHAP, DbType.String);
      	    TEMP_HOADONNHAPKHOList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public TEMP_HOADONNHAPKHO SelectByKey(string MAHOADONNHAP, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MAHOADONNHAP", MAHOADONNHAP, DbType.String);
      	    TEMP_HOADONNHAPKHOList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public TEMP_HOADONNHAPKHOList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    TEMP_HOADONNHAPKHOList list =  SelectListByCommand(command);
	        return list;
        }

         public TEMP_HOADONNHAPKHOList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    TEMP_HOADONNHAPKHOList list =  SelectListByCommand(command);
	        return list;
        }

        public TEMP_HOADONNHAPKHOList SelectByCondition(TEMP_HOADONNHAPKHOConditionForm condt)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM TEMP_HOADONNHAPKHO " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            TEMP_HOADONNHAPKHOList list = SelectListByCommand(command);
            return list;
        }

        private void DeleteByCondition(TEMP_HOADONNHAPKHOConditionForm condt, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM TEMP_HOADONNHAPKHO " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(TEMP_HOADONNHAPKHOConditionForm obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetMAHOADONNHAP)
	{
    	    s += " AND MAHOADONNHAP = @MAHOADONNHAP ";
	}
	if (obj.IsSetTHANHTIENCHUACK_HD)
	{
    	    s += " AND THANHTIENCHUACK_HD = @THANHTIENCHUACK_HD ";
	}
	if (obj.IsSetCHIETKHAUTHEOTIEN_HD)
	{
    	    s += " AND CHIETKHAUTHEOTIEN_HD = @CHIETKHAUTHEOTIEN_HD ";
	}
	if (obj.IsSetSOTIENTHANHTOAN_HD)
	{
    	    s += " AND SOTIENTHANHTOAN_HD = @SOTIENTHANHTOAN_HD ";
	}
	if (obj.IsSetSOTIENCHI)
	{
    	    s += " AND SOTIENCHI = @SOTIENCHI ";
	}
	if (obj.IsSetLOAIHOADON)
	{
    	    s += " AND LOAIHOADON = @LOAIHOADON ";
	}
	if (obj.IsSetHANTHANHTOAN)
	{
    	    s += " AND HANTHANHTOAN = @HANTHANHTOAN ";
	}
	if (obj.IsSetGHICHU_HD)
	{
    	    s += " AND GHICHU_HD = @GHICHU_HD ";
	}
	if (obj.IsSetDATHANHTOAN)
	{
    	    s += " AND DATHANHTOAN = @DATHANHTOAN ";
	}
	if (obj.IsSetNGUOITAO)
	{
    	    s += " AND NGUOITAO = @NGUOITAO ";
	}
	if (obj.IsSetNGAYTAOHOADON)
	{
    	    s += " AND NGAYTAOHOADON = @NGAYTAOHOADON ";
	}
	if (obj.IsSetISDELETE)
	{
    	    s += " AND ISDELETE = @ISDELETE ";
	}
	if (obj.IsSetMANHACUNGCAP)
	{
    	    s += " AND MANHACUNGCAP = @MANHACUNGCAP ";
	}
	if (obj.IsSetMACA)
	{
    	    s += " AND MACA = @MACA ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, TEMP_HOADONNHAPKHOConditionForm obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetMAHOADONNHAP)
            {
            	DalTools.addDbDataParameter(command, "MAHOADONNHAP", obj.MAHOADONNHAP, DbType.String); 
            }
            if (obj.IsSetTHANHTIENCHUACK_HD)
            {
            	DalTools.addDbDataParameter(command, "THANHTIENCHUACK_HD", obj.THANHTIENCHUACK_HD, DbType.Decimal); 
            }
            if (obj.IsSetCHIETKHAUTHEOTIEN_HD)
            {
            	DalTools.addDbDataParameter(command, "CHIETKHAUTHEOTIEN_HD", obj.CHIETKHAUTHEOTIEN_HD, DbType.Decimal); 
            }
            if (obj.IsSetSOTIENTHANHTOAN_HD)
            {
            	DalTools.addDbDataParameter(command, "SOTIENTHANHTOAN_HD", obj.SOTIENTHANHTOAN_HD, DbType.Decimal); 
            }
            if (obj.IsSetSOTIENCHI)
            {
            	DalTools.addDbDataParameter(command, "SOTIENCHI", obj.SOTIENCHI, DbType.Decimal); 
            }
            if (obj.IsSetLOAIHOADON)
            {
            	DalTools.addDbDataParameter(command, "LOAIHOADON", obj.LOAIHOADON, DbType.Int32); 
            }
            if (obj.IsSetHANTHANHTOAN)
            {
            	DalTools.addDbDataParameter(command, "HANTHANHTOAN", obj.HANTHANHTOAN, DbType.DateTime); 
            }
            if (obj.IsSetGHICHU_HD)
            {
            	DalTools.addDbDataParameter(command, "GHICHU_HD", obj.GHICHU_HD, DbType.String); 
            }
            if (obj.IsSetDATHANHTOAN)
            {
            	DalTools.addDbDataParameter(command, "DATHANHTOAN", obj.DATHANHTOAN, DbType.Boolean); 
            }
            if (obj.IsSetNGUOITAO)
            {
            	DalTools.addDbDataParameter(command, "NGUOITAO", obj.NGUOITAO, DbType.String); 
            }
            if (obj.IsSetNGAYTAOHOADON)
            {
            	DalTools.addDbDataParameter(command, "NGAYTAOHOADON", obj.NGAYTAOHOADON, DbType.DateTime); 
            }
            if (obj.IsSetISDELETE)
            {
            	DalTools.addDbDataParameter(command, "ISDELETE", obj.ISDELETE, DbType.Boolean); 
            }
            if (obj.IsSetMANHACUNGCAP)
            {
            	DalTools.addDbDataParameter(command, "MANHACUNGCAP", obj.MANHACUNGCAP, DbType.Int32); 
            }
            if (obj.IsSetMACA)
            {
            	DalTools.addDbDataParameter(command, "MACA", obj.MACA, DbType.Int32); 
            }
        }
        #endregion
    }
}